{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2bd52223",
   "metadata": {},
   "source": [
    "# 人脸分析函数封装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "727325e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d720dbf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_analyze(API_key,API_secret,face_tokens,landmark=1,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸分析接口函数(face_tokens)\"\"\"\n",
    "    face_analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'face_tokens':face_tokens,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max\n",
    "    }\n",
    "    r = requests.post(face_analyze_url,payload)\n",
    "    return r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "89ef4a61",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'\n",
    "face_token = '4f1d074978a129203ebad36eeee173d9'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9ece57b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 89,\n",
       " 'request_id': '1647429887,07e6c650-85c7-4b8a-9cbf-31fd2dfc3415',\n",
       " 'faces': [{'landmark': {'mouth_upper_lip_left_contour2': {'y': 174, 'x': 180},\n",
       "    'mouth_upper_lip_top': {'y': 174, 'x': 194},\n",
       "    'mouth_upper_lip_left_contour1': {'y': 174, 'x': 190},\n",
       "    'left_eye_upper_left_quarter': {'y': 128, 'x': 165},\n",
       "    'left_eyebrow_lower_middle': {'y': 119, 'x': 169},\n",
       "    'mouth_upper_lip_left_contour3': {'y': 176, 'x': 183},\n",
       "    'left_eyebrow_lower_left_quarter': {'y': 119, 'x': 161},\n",
       "    'left_eye_bottom': {'y': 132, 'x': 170},\n",
       "    'right_eyebrow_lower_left_quarter': {'y': 117, 'x': 211},\n",
       "    'nose_contour_left1': {'y': 130, 'x': 186},\n",
       "    'mouth_lower_lip_right_contour1': {'y': 180, 'x': 208},\n",
       "    'mouth_lower_lip_left_contour2': {'y': 182, 'x': 178},\n",
       "    'mouth_lower_lip_left_contour3': {'y': 187, 'x': 185},\n",
       "    'mouth_lower_lip_bottom': {'y': 188, 'x': 196},\n",
       "    'contour_chin': {'y': 214, 'x': 197},\n",
       "    'contour_left9': {'y': 212, 'x': 185},\n",
       "    'mouth_lower_lip_top': {'y': 183, 'x': 195},\n",
       "    'right_eyebrow_upper_middle': {'y': 110, 'x': 218},\n",
       "    'left_eyebrow_left_corner': {'y': 120, 'x': 154},\n",
       "    'contour_left6': {'y': 190, 'x': 157},\n",
       "    'right_eye_lower_right_quarter': {'y': 129, 'x': 223},\n",
       "    'right_eye_bottom': {'y': 130, 'x': 218},\n",
       "    'contour_left7': {'y': 199, 'x': 165},\n",
       "    'contour_right8': {'y': 204, 'x': 220},\n",
       "    'contour_left5': {'y': 179, 'x': 151},\n",
       "    'contour_left4': {'y': 168, 'x': 146},\n",
       "    'contour_left3': {'y': 156, 'x': 143},\n",
       "    'contour_left2': {'y': 145, 'x': 142},\n",
       "    'contour_left1': {'y': 133, 'x': 140},\n",
       "    'left_eye_lower_left_quarter': {'y': 132, 'x': 165},\n",
       "    'contour_right1': {'y': 128, 'x': 246},\n",
       "    'right_eye_pupil': {'y': 127, 'x': 218},\n",
       "    'contour_right2': {'y': 139, 'x': 246},\n",
       "    'mouth_left_corner': {'y': 175, 'x': 172},\n",
       "    'contour_right4': {'y': 163, 'x': 244},\n",
       "    'contour_right7': {'y': 195, 'x': 228},\n",
       "    'contour_left8': {'y': 206, 'x': 174},\n",
       "    'nose_right': {'y': 157, 'x': 210},\n",
       "    'right_eye_upper_right_quarter': {'y': 125, 'x': 223},\n",
       "    'nose_tip': {'y': 160, 'x': 194},\n",
       "    'contour_right5': {'y': 175, 'x': 240},\n",
       "    'nose_contour_lower_middle': {'y': 166, 'x': 195},\n",
       "    'right_eye_top': {'y': 125, 'x': 217},\n",
       "    'right_eye_right_corner': {'y': 127, 'x': 227},\n",
       "    'mouth_upper_lip_right_contour1': {'y': 173, 'x': 199},\n",
       "    'mouth_upper_lip_right_contour2': {'y': 172, 'x': 209},\n",
       "    'right_eyebrow_lower_right_quarter': {'y': 116, 'x': 226},\n",
       "    'contour_right9': {'y': 211, 'x': 210},\n",
       "    'mouth_right_corner': {'y': 172, 'x': 218},\n",
       "    'right_eye_lower_left_quarter': {'y': 130, 'x': 213},\n",
       "    'left_eyebrow_right_corner': {'y': 119, 'x': 185},\n",
       "    'left_eyebrow_lower_right_quarter': {'y': 119, 'x': 177},\n",
       "    'right_eye_center': {'y': 128, 'x': 218},\n",
       "    'left_eye_pupil': {'y': 129, 'x': 170},\n",
       "    'left_eye_upper_right_quarter': {'y': 128, 'x': 176},\n",
       "    'nose_left': {'y': 158, 'x': 179},\n",
       "    'right_eyebrow_lower_middle': {'y': 116, 'x': 218},\n",
       "    'left_eye_center': {'y': 130, 'x': 170},\n",
       "    'left_eye_lower_right_quarter': {'y': 132, 'x': 175},\n",
       "    'right_eyebrow_left_corner': {'y': 117, 'x': 203},\n",
       "    'right_eye_left_corner': {'y': 130, 'x': 209},\n",
       "    'left_eyebrow_upper_left_quarter': {'y': 114, 'x': 160},\n",
       "    'left_eye_left_corner': {'y': 130, 'x': 161},\n",
       "    'left_eye_right_corner': {'y': 131, 'x': 180},\n",
       "    'right_eyebrow_upper_left_quarter': {'y': 112, 'x': 210},\n",
       "    'right_eyebrow_right_corner': {'y': 116, 'x': 233},\n",
       "    'right_eye_upper_left_quarter': {'y': 126, 'x': 212},\n",
       "    'left_eyebrow_upper_middle': {'y': 113, 'x': 169},\n",
       "    'right_eyebrow_upper_right_quarter': {'y': 111, 'x': 226},\n",
       "    'mouth_lower_lip_right_contour3': {'y': 185, 'x': 206},\n",
       "    'nose_contour_left2': {'y': 150, 'x': 182},\n",
       "    'nose_contour_left3': {'y': 162, 'x': 186},\n",
       "    'nose_contour_right1': {'y': 130, 'x': 202},\n",
       "    'nose_contour_right2': {'y': 149, 'x': 206},\n",
       "    'mouth_lower_lip_right_contour2': {'y': 180, 'x': 213},\n",
       "    'mouth_upper_lip_bottom': {'y': 176, 'x': 195},\n",
       "    'contour_right6': {'y': 185, 'x': 235},\n",
       "    'nose_contour_right3': {'y': 162, 'x': 203},\n",
       "    'left_eye_top': {'y': 127, 'x': 171},\n",
       "    'mouth_upper_lip_right_contour3': {'y': 174, 'x': 206},\n",
       "    'left_eyebrow_upper_right_quarter': {'y': 114, 'x': 177},\n",
       "    'contour_right3': {'y': 151, 'x': 246},\n",
       "    'mouth_lower_lip_left_contour1': {'y': 181, 'x': 183}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '4f1d074978a129203ebad36eeee173d9'}]}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_analyze(API_key,API_secret,face_token)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e92ff11",
   "metadata": {},
   "source": [
    "# 人脸对比函数封装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b8cb2476",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_compare(API_key,API_secret,image_url1,image_url2):\n",
    "    \"\"\"face++ 人脸对比接口函数(图片链接)\"\"\"\n",
    "    face_compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url1':image_url1,\n",
    "        'image_url2':image_url2,\n",
    "    }\n",
    "    r = requests.post(face_compare_url,payload)\n",
    "    return r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "83cd2e38",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_url1 = 'https://happymag.tv/wp-content/uploads/2020/08/M.jpg'\n",
    "image_url2 = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.608FcsxY1LPFlvXFiWNu5QHaGr?pid=ImgDet&rs=1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cdd04cc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647430616,f8c68617-503c-46e3-9acf-5b86fd2ecb9d',\n",
       " 'time_used': 3120,\n",
       " 'confidence': 90.213,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975},\n",
       " 'faces1': [{'face_token': 'd0f87875c526bbda471262984212b260',\n",
       "   'face_rectangle': {'top': 179, 'left': 238, 'width': 336, 'height': 336}}],\n",
       " 'faces2': [{'face_token': '46d047c080f3c350b41fb1d3712cfd36',\n",
       "   'face_rectangle': {'top': 139, 'left': 119, 'width': 251, 'height': 251}}],\n",
       " 'image_id1': 'gg31DGIQb5WFJmobXPDJdw==',\n",
       " 'image_id2': 'XsTiSuytBBp97936infoeA=='}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_compare(API_key,API_secret,image_url1,image_url2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e8e7846",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
